/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Clazzteach;
import me.zhengjie.modules.system.domain.Teach;
import me.zhengjie.modules.system.service.ClazzteachService;
import me.zhengjie.modules.system.domain.vo.ClazzteachQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2024-12-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "班级教材管理")
@RequestMapping("/api/clazzteach")
public class ClazzteachController {

    private final ClazzteachService clazzteachService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('clazzteach:list')")
    public void exportClazzteach(HttpServletResponse response, ClazzteachQueryCriteria criteria) throws IOException {
        clazzteachService.download(clazzteachService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询班级教材")
    @ApiOperation("查询班级教材")
//    @PreAuthorize("@el.check('clazzteach:list')")
    public ResponseEntity<PageResult<Clazzteach>> queryClazzteach(ClazzteachQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(clazzteachService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/getClazzTeachList")
    @Log("查询课程教材")
    @ApiOperation("查询课程教材")
    public ResponseEntity<List<Teach>> getClazzTeachList(ClazzteachQueryCriteria criteria){
        List<Teach> teachList = clazzteachService.getClazzTeachList(criteria);
        return new ResponseEntity<>(teachList,HttpStatus.OK);
    }

    @GetMapping("/getStudentClazzTeach")
    @Log("查询学生班级教材")
    @ApiOperation("查询学生班级教材")
    public ResponseEntity<List<Teach>> getStudentClazzTeach(ClazzteachQueryCriteria criteria){
        List<Teach> teachList = clazzteachService.getStudentClazzTeach(criteria);
        return new ResponseEntity<>(teachList,HttpStatus.OK);
    }


    @PostMapping
    @Log("新增班级教材")
    @ApiOperation("新增班级教材")
//    @PreAuthorize("@el.check('clazzteach:add')")
    public ResponseEntity<Object> createClazzteach(@Validated @RequestBody ClazzteachQueryCriteria criteria){

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazzId",criteria.getClazzId());
        clazzteachService.remove(queryWrapper);
        for(Integer item:criteria.getTeachIds()){
            Clazzteach clazzteach = new Clazzteach();
            clazzteach.setClazzId(criteria.getClazzId());
            clazzteach.setTeachId(item);
            clazzteachService.create(clazzteach);
        }

        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改班级教材")
    @ApiOperation("修改班级教材")
//    @PreAuthorize("@el.check('clazzteach:edit')")
    public ResponseEntity<Object> updateClazzteach(@Validated @RequestBody Clazzteach resources){
        clazzteachService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除班级教材")
    @ApiOperation("删除班级教材")
//    @PreAuthorize("@el.check('clazzteach:del')")
    public ResponseEntity<Object> deleteClazzteach(@RequestBody List<Integer> ids) {
        clazzteachService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}